//
//  SearchRecord.m
//  Amily
//
//  Created by hao on 16/3/12.
//  Copyright © 2016年 Amily. All rights reserved.
//

#import "SearchRecord.h"
NSString *const SearchRecordFilePath = @"SearchRecord";
NSString *const SearchRecordPlist   = @"searchRecord.plist";
NSString *const SearchPlistRootKey = @"search";


@implementation SearchRecord

+ (SearchRecord *)shareSearchRecord
{
    static SearchRecord * searchRecord = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        searchRecord = [[SearchRecord alloc]init];
    });
    return searchRecord;
}

- (void)saveSearchRecord:(NSArray *)array

{
    [self setSearchValue:array key:SearchPlistRootKey];
}

- (void)setSearchValue:(id)value key:(NSString *)key
{
    [self setSearchValue:value key:key sub:nil];
}

- (void)setSearchValue:(id)value key:(NSString *)key sub:(NSString *)subkey
{

    [ALCacheData set:value key:key sub:subkey file:SearchRecordFilePath plist:SearchRecordPlist];
}

- (void)removeSearchRecord
{
    [ALCacheData removeFile:SearchRecordFilePath];
    [SearchRecord createSearchRecord];
}

- (NSArray *)getSearchRecord
{

    return [self attribute:SearchPlistRootKey];
}

- (NSArray *)attribute:(NSString *)key
{
    return [ALCacheData objectForPlistKey:key file:SearchRecordFilePath plist:SearchRecordPlist];
}

+ (void)createSearchRecord
{
    if (![ALCacheData isExists:SearchRecordFilePath]) {
        [ALCacheData writeData:@{SearchPlistRootKey:@[]} file:SearchRecordFilePath plist:SearchRecordPlist];
    }
}


@end
